<?php

namespace Zilla\StoreBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Zilla\FrontBundle\Entity\Store;

class DefaultController extends Controller
{
    /**
     *
     * @return Response 
     */
    public function createAction()
    {
        $proWinker = $this->get('security.context')->getToken()->getUser();
        $store = new Store();
        $store->setOwner($proWinker);
        $store->setLastModifiedDate(new \DateTime());
        $store->setLastNotifiedDate(new \DateTime());
        $form = $this->get('store.form');
        $formHandler = $this->get('store.form.handler');
        if ($formHandler->process($store)) {
            return $this->render('ZillaStoreBundle:Default:created.html.twig');
        }
        return $this->render('ZillaStoreBundle:Default:index.html.twig', array('form' => $form->createView()));
    }

    /**
     * @param string $alias
     * @return Response 
     */
    public function editAction($alias)
    {
        $themes=$this->get('doctrine')->getRepository('ZillaFrontBundle:Theme')->findAll();
        $store=$this->get('doctrine')->getRepository('ZillaFrontBundle:Store')->findOneBy(array('alias' => $alias));
        $proWinkers=$store->getProFollowers();
        $form = $this->get('store.form');
        $formHandler = $this->get('store.form.handler');
        if ($formHandler->process($store)) {
            return $this->render('ZillaStoreBundle:Default:edited.html.twig');
        }
        return $this->render('ZillaStoreBundle:Default:editStore.html.twig', 
                array('form' => $form->createView(),
                    'store' => $store,
                    'themes' => $themes));
    }
}
